package com.hycc.jsoup;

import lombok.extern.log4j.Log4j2;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

@Log4j2
public class JsoupDemo {

    private URL url; //HTML网址
    private String way = "get"; //爬取方式 默认get
    private Document dom;

    public JsoupDemo(URL url) {
        this.url = url;
    }

    public JsoupDemo(URL url, String way) {
        this.url = url;
        if ("post".toLowerCase().equals(way)) {
            this.way = way.toLowerCase();
        }
    }

    public JsoupDemo(URL url, int way) {
        this.url = url;
        if (2 == way) {
            this.way = "post";
        }
    }

    /*
     * 抓取网页
     * */
    public int crawl() {
        int ret = 0;

        //封装请求头，连接URL，返回DOM
        ret = packageHeader();
        if (0 != ret) {
            log.info("封装请求头失败");
            return ret;
        }

        //解析DOM
        ret = parse();
        if (0 != ret) {
            log.info("解析失败");
            return ret;
        }

        return ret;
    }

    /*
     * 封装请求头，连接URL，返回DOM
     * */
    private int packageHeader() {
        try {
            //封装请求头
            Connection connect = Jsoup.connect(url.toURI().toString()).timeout(2000);
            Map<String, String> headers = new HashMap<>();
            headers.put("user-agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36");
            headers.put("accept","text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9");
            headers.put("accept-encoding","gzip, deflate, br");
            headers.put("accept-language","zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7");
            headers.put("method",way);
            try {
                //连接URL，返回DOM
                dom = connect.headers(headers).get();
            } catch (IOException e) {
                log.info("获取DOM失败+{}",e);
                return -1;
            }
        } catch (URISyntaxException e) {
            log.info("URL转URI异常+{}",e);
            return -1;
        }

        return 0;
    }

    /*
     * 解析DOM
     * */
    private int parse() {

        if(null==dom){
            log.info("DOM无效");
            return -1;
        }
        //查看HTML源码，分析DOM，找到element
        Element row = dom.getElementsByClass("row").get(0);
        Elements captions = row.getElementsByClass("caption");
        //遍历元素
        for (Element caption : captions) {
            Element a = caption.getElementsByTag("a").get(0);
            Element p = caption.getElementsByTag("p").get(0);
            String title = a.ownText() + "：";
            String urlStr = a.attr("abs:href"); //abs:href 绝对路径
            String demo = p.text();


            log.info(title);
            log.info(demo);
            log.info(urlStr);
            log.info("");

        }
        return 0;
    }
}

class JsoupDemoTest {

    public static void main(String[] args) throws MalformedURLException {
        String url = ""; //解析网址

        JsoupDemo jsoupDemo = new JsoupDemo(new URL(url));
        jsoupDemo.crawl();
    }
}